package com.obuge.yfly.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.obuge.yfly.Interceptor.CustomerException;
import com.obuge.yfly.dto.Login.LoginReqDto;
import com.obuge.yfly.entity.MySession;
import com.obuge.yfly.entity.User;
import com.obuge.yfly.mapper.MySessionMapper;
import com.obuge.yfly.service.UserService;
import com.obuge.yfly.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
* @author yfly
* @description 针对表【user】的数据库操作Service实现
* @createDate 2022-06-08 13:51:15
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
implements UserService{
    @Autowired
    UserMapper userMapper;
    @Autowired
    MySessionMapper mySessionMapper;
    private final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
    @Override
    public MySession login(LoginReqDto dto){
        User user = userMapper.selectOne(new QueryWrapper<User>().eq("userName",dto.getUserName()));
        if(user != null){
            if(user.getPassWord().equals(dto.getPassWord())){
                //token
                Long token = IdWorker.getId();
                MySession mySession = new MySession();
                mySession.setId(token);
                mySession.setUserId(user.getId());
                mySession.setUserName(user.getName());
                mySession.setPlatform(dto.getPlatform());
                Date date = new Date();
                mySession.setInTime(format.format(date));
                mySessionMapper.insert(mySession);
                return mySession;
            }else{
                throw new CustomerException("输入密码错误");
            }
        }else{
            throw new CustomerException("用户不存在,请联系管理员处理！");
        }

    }
}
